package icraf.carbon.gui;

import icraf.carbon.core.AbacusContainer;
import icraf.carbon.core.CostBenefitUnit;
import icraf.carbon.core.Project;
import icraf.carbon.gui.MenuFactory.MenuProperty;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListModel;
import javax.swing.SwingConstants;

public class InputCBUPanel extends JPanel {
	public class Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (MenuProperty.LIST_ADD.isAction(e)) {
				addNewCBU();
				project.getLandCoverList().setUpdated(true);
			} else if (MenuProperty.LIST_REMOVE.isAction(e)) {
				remove();
			} else if (e.getSource().equals(inputLabel)) {
				int idx = inputLabel.getSelectedIndex();
				if(idx >= 0 && idx < Project.defaultCostBenefitUnit.length) {
					inputDesc.setText(Project.defaultCostBenefitUnit[idx].getDescription());
				}
			}
		}



	}

	class CBUTableModel extends MatrixTableModel {
		private static final long serialVersionUID = 8968868974903218229L;
		private String[] headers = {
				MenuProperty.PROJECT_COSTB_UNIT.getLabel(),
				MenuProperty.INPUT_DESC.getLabel()};
		private NumberRowHeader tableListModel= new NumberRowHeader(this);
		
		@Override
		public Class<?> getColumnClass(int column) {
			return String.class;
		}

		public void addNew(CostBenefitUnit cbu) {
			if(project == null) return;
			ArrayList<CostBenefitUnit> list = project.getCostBenefitUnitList();
			int n = list.size();
			list.add(cbu);
			this.fireTableRowsInserted(n, n);
		}

		@Override
		public int getColumnCount() {
			return headers.length;
		}

		public String getColumnName(int col) {
			return headers[col];
		}

		@Override
		public int getRowCount() {
			if(project == null) return 0;
			return project.getCostBenefitUnitList().size();
		}

		@Override
		public Object getValueAt(int row, int column) {
			CostBenefitUnit cbu = project.getCostBenefitUnitList().get(row);
			switch(column) {
				case 0: return cbu.getLabel(); 
				case 1: return cbu.getDescription();
			}
			return null;
		}

		public boolean isCellEditable(int row, int col) {
			return true;
		}

		public void setValueAt(Object value, int row, int col) {
			if(value == null) return;
			CostBenefitUnit cbu = project.getCostBenefitUnitList().get(row);
			if (col == 0) {
				cbu.setLabel(value.toString());
			} else if (col == 1) {
				cbu.setDescription(value.toString());
			}
			fireTableCellUpdated(row, col);
		}

		@Override
		public ListModel<Object> getRowHeaderModel() {
			return tableListModel;
		}

		public void delete(int[] sel) {
			ArrayList<CostBenefitUnit> ls = new ArrayList<CostBenefitUnit>();
			for(int i: sel) {
				ls.add(project.getCostBenefitUnitList().get(i));
			}
			for(CostBenefitUnit z: ls) {
				project.getCostBenefitUnitList().remove(z);
			}
			fireTableDataChanged();
		}



	}
	


	private static final long serialVersionUID = -5844291625322786356L;
	private CBUTableModel cbuTableModel = new CBUTableModel();
	private Project project;
	private Listener listener = new Listener();
	private MatrixPanel matrixPanel;
	private ListToolBar listToolBar;
	
	public InputCBUPanel() {
		super();
		
		JPanel mainPanel = new JPanel(new BorderLayout());
		matrixPanel = new MatrixPanel(cbuTableModel);
		JTable zTab = matrixPanel.getTable();
		zTab.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		mainPanel.add(matrixPanel);
		
		listToolBar = new ListToolBar(listener);
		listToolBar.setFloatable(false);
		mainPanel.add(listToolBar, BorderLayout.NORTH);
		
		this.setLayout(new BorderLayout(5, 5));
		this.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
		this.add(mainPanel, BorderLayout.CENTER);
		this.validate();
		
		inputCBUPanel = createInputLabelPanel();
	}

	public void addNewCBU() {
		int opt = JOptionPane.showConfirmDialog(this, inputCBUPanel,
				MenuProperty.PROJECT_COSTB_UNIT_ADD.getLabel(),
				JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
		if(opt != JOptionPane.OK_OPTION)
			return;
		CostBenefitUnit cbu = new CostBenefitUnit(inputLabel.getSelectedItem().toString(), inputDesc.getText());
		cbuTableModel.addNew(cbu);
	}

	private void remove() {
		
		int sel[] = matrixPanel.getTable().getSelectedRows();
		if(sel.length <= 0)
			return;
		if(project.getCostBenefitUnitList().size() == 1) {
			JOptionPane.showMessageDialog(this, 
					MenuProperty.PROJECT_CBU_DELETE_ERROR.getAccessibleDescription(),
					MenuProperty.PROJECT_CBU_DELETE_ERROR.getLabel(),
					JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		int opt = JOptionPane.showConfirmDialog(this, 
				MenuProperty.PROJECT_CBU_DELETE.getAccessibleDescription(),
				MenuProperty.PROJECT_CBU_DELETE.getLabel(), 
				JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		if(opt !=  JOptionPane.YES_OPTION)
			return;
		cbuTableModel.delete(sel);
	}

	public AbacusContainer getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
		NumberRowHeader m = (NumberRowHeader)cbuTableModel.getRowHeaderModel();
		m.fireContentChange(project.getCostBenefitUnitList().size());
		cbuTableModel.fireTableStructureChanged();
		matrixPanel.getTable().getColumnModel().getColumn(1).setPreferredWidth(300);
	}
	
	private JComboBox<?> inputLabel;
	private JTextArea inputDesc;
	private JPanel inputCBUPanel; 

	private JPanel createInputLabelPanel() {
		JLabel l1 = new JLabel(MenuProperty.INPUT_LABEL.getLabel() + ":");
		JLabel l2 = new JLabel(MenuProperty.INPUT_DESC.getLabel() + ":");
		l2.setHorizontalAlignment(SwingConstants.LEFT);

		inputLabel = new JComboBox<Object>(Project.defaultCostBenefitUnit);
		inputLabel.setEditable(true);
		inputLabel.addActionListener(listener);
		inputLabel.setSelectedItem(null);
		inputLabel.setPreferredSize(new Dimension(200, 20));
		
		inputDesc = new JTextArea(5, 30);
		inputDesc.setBorder(inputLabel.getBorder());
		inputDesc.setLineWrap(true);
		
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(2, 2, 2, 2);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 0;
		panel.add(l1, c);
		c.gridx = 1;
		panel.add(inputLabel, c);
		c.gridx = 0;
		c.gridy = 1;
		panel.add(l2, c);
		c.gridx = 1;
		panel.add(inputDesc, c);

		return panel;
		
	}

}
